package com.banciyuan.bcywebview.biz.setting.bansetting;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.share.block.BlockHelper;
import com.banciyuan.bcywebview.biz.share.block.IBlockHelper;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DiscoverData;

/**
 * Created by squall on 2016/3/7.
 */
public class BlockCircleListAdapter extends BaseAdapter {

    private Context mContext;
    private List<DiscoverData> ulist = new ArrayList<>();

    public BlockCircleListAdapter(Context mContext, List<DiscoverData> ulist) {
        this.mContext = mContext;
        this.ulist = ulist;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 1 : 0;
    }

    @Override
    public int getCount() {
        return ulist.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return ulist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        private ImageView civTouXiang;
        private TextView tvGuanzhuName;
        private TextView ivRemove;


        public ViewHolder(View convertView) {
            civTouXiang = (ImageView) convertView.findViewById(R.id.civ_touxiang);
            tvGuanzhuName = (TextView) convertView.findViewById(R.id.tv_guanzhu_name);
            ivRemove = (TextView) convertView.findViewById(R.id.tv_remove);
        }
    }

    private class ViewEmpty {
        private LinearLayout block_empty;
        private ImageView empty_img;

        private ViewEmpty(View convertView) {
            block_empty = (LinearLayout) convertView.findViewById(R.id.block_empty);
            empty_img = (ImageView) convertView.findViewById(R.id.empty_img);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewEmpty viewEmpty = null;
        if (null == convertView) {
            if (getItemViewType(position) == 0) {
                convertView = View.inflate(mContext, R.layout.block_circle_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                convertView = View.inflate(mContext, R.layout.block_circle_empty, null);
                viewEmpty = new ViewEmpty(convertView);
                convertView.setTag(viewEmpty);
            }

        } else {
            if (getItemViewType(position) == 0) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                viewEmpty = (ViewEmpty) convertView.getTag();
            }
        }
        if (getItemViewType(position) == 0 && viewHolder != null) {
            final DiscoverData discoverData = ulist.get(position - 1);
            ImageLoader.getInstance().displayImage(discoverData.getCover(), viewHolder.civTouXiang, ImageOptions.optionHead);
            viewHolder.tvGuanzhuName.setText(discoverData.getName());
            viewHolder.ivRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IBlockHelper iBlockHelper = new IBlockHelper() {
                        @Override
                        public void getSucc(String json) {
                            ulist.remove(position - 1);
                            notifyDataSetChanged();
                        }

                        @Override
                        public void getFail() {
                            MyToast.show(mContext, mContext.getString(R.string.operation_fail));
                        }
                    };
                    BlockHelper blockHelper = new BlockHelper(iBlockHelper, mContext);
                    blockHelper.delBlockCircle(UserDataHelper.getInstance(mContext).getToken(), discoverData.getId(), discoverData.getType());
                }
            });
        } else {
            if (viewEmpty != null) {
                if (ulist.isEmpty()) {
                    viewEmpty.block_empty.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(mContext.getString(R.string.block_circle_empty_guide_image), viewEmpty.empty_img, ImageOptions.optionsCopy);
                } else {
                    viewEmpty.block_empty.setVisibility(View.GONE);
                }
            }
        }

        return convertView;
    }
}
